package com.onefinger.services;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;

import com.onefinger.domain.Category;
import com.onefinger.domain.Constant;
import com.onefinger.domain.Item;
import com.onefinger.domain.Language;
import com.onefinger.domain.Subcategory;
import com.onefinger.dto.LookupTables;

public class LookupTableController implements ILookupTableController {

	@Autowired
	IItemService itemService;

	@Autowired
	ICategoryService categoryService;
	
	@Autowired
	ISubcategoryService subcategoryService;

	@Autowired
	ILanguageService languageService;

	@Autowired
	IResourceService resourceService;

    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Item.class, new ItemCustomEditor());
    }

	
	@Override
	public LookupTables getLookupTables( String languageid ) {
		LookupTables lookupTables = new LookupTables();
		
		List<Category> categories = categoryService.list();
		lookupTables.setCategories( new ArrayList<Category>() );
		for (Category category : categories) {
			Category cloneCategory = category.clone();
			cloneCategory.setName(resourceService.getLocale(languageid, Constant.CATEGORY + Constant.NAME + cloneCategory.getName()));
			lookupTables.getCategories().add(cloneCategory);
		}
		
		List<Subcategory> subcategories = subcategoryService.list();
		lookupTables.setSubcategories( new ArrayList<Subcategory>() );
		for (Subcategory subcategory : subcategories) {
			Subcategory cloneSubcategory = subcategory.clone();
			cloneSubcategory.setName(resourceService.getLocale(languageid, Constant.SUBCATEGORY + Constant.NAME + cloneSubcategory.getName()));
			cloneSubcategory.setShortDescription(resourceService.getLocale(languageid, Constant.SUBCATEGORY + Constant.SHORT_DESCRIPTION + cloneSubcategory.getShortDescription()));
			lookupTables.getSubcategories().add(cloneSubcategory);
		} 

		List<Item> items = itemService.list();
		lookupTables.setItems( new ArrayList<Item>() );
		for (Item item : items) {
			Item cloneItem = item.clone();
			cloneItem.setName( resourceService.getLocale(languageid, Constant.ITEM + Constant.NAME + item.getName()) );
			cloneItem.setShortDescription(resourceService.getLocale(languageid, Constant.ITEM + Constant.SHORT_DESCRIPTION + cloneItem.getShortDescription()));
			cloneItem.setLongDescription(resourceService.getLocale(languageid, Constant.ITEM + Constant.LONG_DESCRIPTION + cloneItem.getLongDescription()));
			lookupTables.getItems().add(cloneItem);
		}
		
		Language language = languageService.find(languageid);
		//language.setName( resourceService.getLocale( languageid, Constant.LANGUAGE + Constant.NAME + language.getName() ) );
		lookupTables.setLanguage(language);

		List<Language> languages = languageService.list();
		lookupTables.setLanguages(new ArrayList<Language>());
		for (Language language2 : languages) {
			Language cloneLanguage = language2.clone();
			cloneLanguage.setName(resourceService.getLocale(languageid, Constant.LANGUAGE + Constant.NAME + cloneLanguage.getName()));
			lookupTables.getLanguages().add(cloneLanguage);
		}
		
		return lookupTables;
	}

}
